<?php
namespace app\index\controller;

use app\Logined;
use think\facade\Db;

class Feelog extends Logined
{
    public function index()
    {
        $db = Db::name('vmoneylog');
        if(!empty($this->param['opera_id']))$db->where('oid', $this->param['opera_id']);
        $paginate = $db->where('up_or_down', 2)->order('add_time asc')->paginate($this->param['limit'])->toArray();

        foreach ($paginate['data'] as &$item){
            $item['opera_collection_info'] = Db::name('opera_collection')->where('opera_collection_id', $item['cid'])->find();
            $item['opera_info'] = Db::name('opera')->where('opera_id', $item['oid'])->find();
        }

        return $this->success('请求成功', [
            'paginate' => $paginate
        ]);
    }

    public function save()
    {
        $userInfo = $this->user_info;
        if($this->param['vmoney'] > $userInfo['v_money']){
            return $this->error('余额不足');
        }
        $vMoney = (int)bcsub($userInfo['v_money'], $this->param['vmoney'], 2);

        Db::name('vmoneylog')->strict(false)->save([
            'user_id'       =>  $this->user_id,
            'add_time'      =>  time(),
            'log_type'      =>  1,
            'up_or_down'    =>  2,
            'change_num'    =>  $this->param['vmoney'],
            'before_num'    =>  $userInfo['v_money'],
            'after_num'     =>  $vMoney,
            'title'         =>  '购买剧集',
            'oid'           =>  $this->param['opera_id'],
            'cid'           =>  $this->param['opera_collection_id']
        ]);
        Db::name('user')->where('user_id', $this->user_id)->update(['v_money'=>$vMoney]);

        return $this->success('保存成功');
    }

}
